<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="title">
  <div class="title__status">
    <h1>Applications</h1>

    <mat-radio-group
      *gioRole="{ anyOf: [{ scope: 'ORGANIZATION', name: 'ADMIN' }] }"
      class="status__radio-group"
      aria-label="Select a status"
      (change)="onStatusChange()"
      [(ngModel)]="this.currentStatus"
    >
      <mat-radio-button class="status__radio-group__button" *ngFor="let status of statusFilters" [value]="status">
        {{ status }}
      </mat-radio-button>
    </mat-radio-group>
  </div>

  <button
    [routerLink]="'create'"
    mat-raised-button
    color="primary"
    data-testid="add-application"
    *gioPermission="{ anyOf: ['environment-application-c'] }"
  >
    <mat-icon>add</mat-icon> Add application
  </button>
</div>

<gio-table-wrapper
  [length]="nbTotalApplications"
  [filters]="filters"
  (filtersChange)="onFiltersChanged($event)"
  [paginationPageSizeOptions]="[25, 50, 100, 200]"
>
  <table
    mat-table
    matSort
    [dataSource]="filteredTableData"
    [ngClass]="'applications-table-' + currentStatus.toLowerCase()"
    id="applicationsTable"
    aria-label="Application table"
  >
    <!-- Picture Column -->
    <ng-container matColumnDef="applicationPicture">
      <th mat-header-cell *matHeaderCellDef id="applicationPicture" width="1%"></th>
      <td mat-cell *matCellDef="let element">
        <gio-avatar [src]="element.applicationPicture" [name]="element.name" [size]="32" [roundedBorder]="true"></gio-avatar>
      </td>
    </ng-container>

    <!-- Display name Column -->
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef mat-sort-header id="name">Name</th>
      <td mat-cell *matCellDef="let element">
        <a [routerLink]="[element.applicationId]">{{ element.name }}</a>
      </td>
    </ng-container>

    <!-- Type Column -->
    <ng-container matColumnDef="type">
      <th mat-header-cell *matHeaderCellDef id="type" width="15%">Type</th>
      <td mat-cell *matCellDef="let element">
        {{ element.type | titlecase }}
      </td>
    </ng-container>

    <!-- Owner Column -->
    <ng-container matColumnDef="owner">
      <th mat-header-cell *matHeaderCellDef id="owner" width="15%">Owner</th>
      <td mat-cell *matCellDef="let element">{{ element.owner.displayName }}</td>
    </ng-container>

    <!-- Archived at Column -->
    <ng-container matColumnDef="updated_at">
      <th mat-header-cell *matHeaderCellDef mat-sort-header id="updated_at" width="15%">Archived at</th>
      <td mat-cell *matCellDef="let element">{{ element.updated_at | date: 'medium' }}</td>
    </ng-container>

    <!-- Actions Column -->
    <ng-container matColumnDef="actions">
      <th mat-header-cell *matHeaderCellDef id="actions" width="1%"></th>
      <td mat-cell *matCellDef="let element">
        <div class="table__centered-cell">
          <button
            *ngIf="'ACTIVE' === element.status && element.origin === 'MANAGEMENT'"
            [routerLink]="[element.applicationId]"
            mat-icon-button
            aria-label="Button to edit an application"
            matTooltip="Edit application"
          >
            <mat-icon>edit</mat-icon>
          </button>
          <ng-container>
            <button
              *ngIf="'ARCHIVED' === element.status"
              (click)="onRestoreActionClicked(element)"
              mat-icon-button
              aria-label="Button to restore an application"
              matTooltip="Restore application"
            >
              <mat-icon svgIcon="gio:magic-wand"></mat-icon>
            </button>
          </ng-container>
        </div>
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="this.displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: this.displayedColumns"></tr>

    <!-- Row shown when there is no data -->
    <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
      <td class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="this.displayedColumns.length">
        {{ 'ACTIVE' === currentStatus ? 'There is no application (yet).' : 'There is no archived application.' }}
      </td>
    </tr>
  </table>
</gio-table-wrapper>
